﻿using System;
using System.Collections.Generic;
using System.Text;
using System.IO;
using System.Web;
using System.Web.Caching;
using ShareMarking.Systems.Entity;

namespace ShareMarking.Systems.Service
{
    public class siteconfigService
    {
        #region "单例"
        private static siteconfigService service;
        public static siteconfigService Instance
        {
            get
            {
                if (service == null)
                {
                    service = new siteconfigService();
                }
                return service;
            }
        }
        #endregion

        /// <summary>
        ///  读取配置文件
        /// </summary>
        public siteconfig loadConfig()
        {
            siteconfig model = CacheHelper.Get<siteconfig>(DTKeys.CACHE_WEB_CONFIG);
            if (model == null)
            {
                CacheHelper.Insert(DTKeys.CACHE_WEB_CONFIG, loadConfig(Utils.GetXmlMapPath(DTKeys.FILE_SITE_XML_CONFING)),
                    Utils.GetXmlMapPath(DTKeys.FILE_SITE_XML_CONFING));
                model = CacheHelper.Get<siteconfig>(DTKeys.CACHE_WEB_CONFIG);
            }
            return model;
        }

        /// <summary>
        ///  保存配置文件
        /// </summary>
        public siteconfig saveConifg(siteconfig model)
        {
            return saveConifg(model, Utils.GetXmlMapPath(DTKeys.FILE_SITE_XML_CONFING));
        }

        private static object lockHelper = new object();

        /// <summary>
        ///  读取站点配置文件
        /// </summary>
        private siteconfig loadConfig(string configFilePath)
        {
            return (siteconfig)SerializationHelper.Load(typeof(siteconfig), configFilePath);
        }

        /// <summary>
        /// 写入站点配置文件
        /// </summary>
        private siteconfig saveConifg(siteconfig model, string configFilePath)
        {
            lock (lockHelper)
            {
                SerializationHelper.Save(model, configFilePath);
            }
            return model;
        }

    }
}
